<template>
  <view class="g_content">
    <youlanSignIn @shift="shift" :integral="integral" :isIntegral="isIntegral" :checkinDays="checkinDays" :already="data" :supplementary="false" type="sign" lang="zh" @changeMonth="historysign" @change="signDate" />
    <view class="sign-box">
      <view style="color:#e6e6e6;font-size: 32rpx;margin-bottom: 10rpx;">签到规则: </view>
      <view style="color:#e6e6e6;" v-for="(item, index) in sign.split('-')" :key="index">连续签到{{index + 1}}天, 获得+{{item}}积分</view>
      <view style="color:#e6e6e6;">连续签到...天, 获得+{{sign.split('-')[sign.split('-').length-1]}}积分</view>
    </view>
  </view>
</template>

<script>
import youlanSignIn from "@/uni_modules/youlan-SignIn/youlan-SignIn.vue";
import {
  getHistorySignApi,
  getTodayAmountApi,
  getByKeyApi,
  signApi,
} from "@/sheep/api/index";
export default {
  components: {
    youlanSignIn,
  },
  data() {
    return {
      data: [],
      checkinDays: 0, // 连续签到天数
      integral: 0, // 本次签到获取的积分
      isIntegral: true, // 是否显示签到积分模块
      sign: "",
    };
  },
  onLoad() {
    this.historysign();
    this.getTodayAmount();
    this.getSign();
  },
  methods: {
    shift() {
      console.log("用户点击了补签按钮");
    },
    async historysign() {
      this.data = [];
      getHistorySignApi().then((res) => {
        if (res.code == 200) {
          for (const i in res.data) {
            this.data.push(new Date(res.data[i]).getTime());
          }
        } else {
          this.$refs.uToast.show({
            title: "获取签到信息失败",
            type: "error",
          });
        }
      });
    },
    async signDate(v) {
      this.checkinDays = this.checkinDays + 1;
      signApi().then((res) => {
        if (res.code == 200) {
          uni.showModal({
            title: "签到成功",
            content: "获得积分+" + this.integral,
            showCancel: false,
          });
        }
      });
    },
    getTodayAmount() {
      getTodayAmountApi().then((res) => {
        if (res.code == 200) {
          this.integral = res.data;
        } else {
          this.$refs.uToast.show({
            title: "获取今日签到积分失败",
            type: "error",
            showCancel: false,
          });
        }
      });
    },
    async getSign() {
      let res = await getByKeyApi("sign");
      this.sign = res.data[0].configValue;
    },
  },
};
</script>

<style lang="scss">
.sign-box {
  padding: 40rpx;
  color: #666;
  font-size: 28rpx;
  text-align: center;
}
</style>
